package com.x.mvp.base.exlist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;

import com.x.mvp.widget.stickyexpandable.StickyListHeadersAdapter;


/**
 * Created by Administrator on 2017/9/4.
 */

public class ExListViewAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer {
    BaseListViewData data;
    private LayoutInflater inflater;

    public ExListViewAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if (data.getHeaderLayoutId(0) == 0) {
            return new View(inflater.getContext());
        }
        if (convertView == null) {
            BaseViewHolder holder = data.getHeaderHolder(position, inflater);
            holder.bindView(data.getHeaderItem(position));
            return holder.getView();
        }
        BaseViewHolder holder = (BaseViewHolder) convertView.getTag();
        if (holder.getLayoutId() == data.getHeaderLayoutId(position)) {
            holder.bindView(data.getHeaderItem(position));
            return convertView;
        } else {
            holder = data.getHeaderHolder(position, inflater);
            holder.bindView(data.getHeaderItem(position));
        }
        return holder.getView();
    }

    @Override
    public long getHeaderId(int position) {
        return data.getHeaderId(position);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            BaseViewHolder holder = data.getHolder(position, inflater);
            holder.bindView(data.getItem(position));
            return holder.getView();
        }
        BaseViewHolder holder = (BaseViewHolder) convertView.getTag();
        if (holder.getLayoutId() == data.getLayoutId(position)) {
            holder.bindView(data.getItem(position));
            return convertView;
        } else {
            holder = data.getHolder(position, inflater);
            holder.bindView(data.getItem(position));
        }
        return holder.getView();
    }

    @Override
    public Object[] getSections() {
        return data.getSections();
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return data.getPositionForSection(sectionIndex);
    }

    @Override
    public int getSectionForPosition(int position) {
        return data.getSectionForPosition(position);
    }

    public void replace(BaseListViewData data) {
        this.data = data;
        notifyDataSetChanged();
    }
}
